package xcmg.device.web.warehouse;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.delivery.ManualFinishVO;
import xcmg.device.service.vo.warehouse.DeliveryInstructionAddVO;
import xcmg.device.service.vo.warehouse.DeliveryInstructionVO;
import xcmg.device.service.warehouse.DeliveryInstructionService;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;

@RestController
@RequestMapping("/deliveryInstruction")
public class DeliveryInstructionController extends BasicController {
    @Autowired
    private DeliveryInstructionService deliveryInstructionService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "增加配送指令入库记录")
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody DeliveryInstructionAddVO deliveryInstructionAddVO)throws Exception{
        if(null == deliveryInstructionAddVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode errorCode;
        try{
            errorCode = deliveryInstructionService.add(deliveryInstructionAddVO, getUserId(), getOrgId());
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
        return actionResult(errorCode);
    }

//    @RequestMapping(value = "/posting",method = RequestMethod.POST)
//    @ApiOperation(value = "配送指令过账")
//    @FastMappingInfo(needLogin = true)
//    public ActionResult posting(@RequestBody DeliveryInstructionAddVO deliveryInstructionAddVO)throws Exception{
//        if(null == deliveryInstructionAddVO || deliveryInstructionAddVO.getDetailId() == null){
//            return actionResult(ErrorCode.IllegalArument);
//        }
//        ErrorCode errorCode;
//        try{
//            errorCode = deliveryInstructionService.posting(deliveryInstructionAddVO.getDetailId(), getUserId());
//        }catch (Exception e){
//            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
//        }
//        return actionResult(errorCode);
//    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @ApiOperation("配送指令入库记录删除")
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody String[] ids)throws Exception{
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(deliveryInstructionService.delete(Arrays.asList(ids), getOrgId()));
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @ApiOperation(value = "查找单条记录详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id)throws Exception{
        return actionValueResult(deliveryInstructionService.findByPK(id, "id", getOrgId()));
    }

    @RequestMapping(value = "/itemByNo",method = RequestMethod.GET)
    @ApiOperation(value = "查找单条记录详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult itemByNo(String abnormalNo)throws Exception{
        return actionValueResult(deliveryInstructionService.findByPK(abnormalNo, "abnormalNo", getOrgId()));
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation("查询配送指令入库记录列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<DeliveryInstructionVO> condition)throws Exception{
        DeliveryInstructionVO deliveryInstructionVO =condition.getFilters();
        if(deliveryInstructionVO == null){
            deliveryInstructionVO = new DeliveryInstructionVO();
        }
        deliveryInstructionVO.setOrgId(getOrgId());
        condition.setFilters(deliveryInstructionVO);
        PageCommonVO pageCommonVO= deliveryInstructionService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @ApiOperation("导出配送指令入库记录列表")
    @FastMappingInfo(needLogin = true)
    public void export( String[] ids)throws Exception {
        deliveryInstructionService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @ApiOperation("导出全部配送指令入库记录列表")
    @FastMappingInfo(needLogin = true)
    public void exportAll(DeliveryInstructionVO condition)throws Exception {
        condition.setOrgId(getOrgId());
        deliveryInstructionService.exportAll(condition);
    }

    @RequestMapping(value = "/manualFinish", method = RequestMethod.POST)
    @ApiOperation("强制完成单据")
    public ActionResult manualFinish(@RequestBody ManualFinishVO manualFinishVO) throws Exception
    {
        return actionResult(deliveryInstructionService.manualFinish(manualFinishVO.getBillNo(), manualFinishVO.getMaterialsNo(), manualFinishVO.getRowNo(),getOrgId()));
    }

    @RequestMapping(value = "/revoke",method = RequestMethod.POST)
    @ApiOperation(value = "配送指令入库撤销")
    @FastMappingInfo(needLogin = true)
    public ActionResult revoke(@RequestBody DeliveryInstructionAddVO deliveryInstructionAddVO)throws Exception{
        if(null == deliveryInstructionAddVO || deliveryInstructionAddVO.getDetailId() == null){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode errorCode;
        try{
            errorCode = deliveryInstructionService.revoke(deliveryInstructionAddVO.getDetailId());
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
        return actionResult(errorCode);
    }
}
